/* internal/js/reservation/form.js */

function resetFields () {
	document.getElementById('reservationID').value = '';
	document.getElementById('firstname').value = '';
	document.getElementById('lastname').value = '';
	document.getElementById('phone').value = '';
	document.getElementById('numPeople').value = '';
	document.getElementById('createdBy').innerHTML = '';
	document.getElementById('description').value = '';
	document.getElementById('datePlaced').value = '';
	document.getElementById('resDateDay').selectedIndex = '0';
	document.getElementById('resDateMonth').selectedIndex = '0';
	document.getElementById('resDateYear').selectedIndex = '0';
	document.getElementById('resDateHour').selectedIndex = '0';
	document.getElementById('resDateMinute').selectedIndex = '0';
	
	hideFieldMessage('firstnameMessage');
	hideFieldMessage('lastnameMessage');
	hideFieldMessage('phoneMessage');
	hideFieldMessage('numPeopleMessage');
	hideFieldMessage('reservationDateMessage');
	hideFieldMessage('descriptionMessage');
	
	selectResultRow(0);
}

function loadPage (day, month, year, type) {
	listOperationHour();
	reportReservationByDate(day, month, year, type);
	//listAllReservation();
}


/* To build result table */

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th>Name</th>' +
	           '<th width="160">Number of People</th>' +
	           '<th>Date</th>' +
	           '<th>Time</th>' +
	           '</tr>';
	return html;
}


function buildTable (reservations) {
	var html = startResultsTable();
	var count = reservations.childNodes.length;
	var ids = new Array(count);
	for (var loop = 0; loop < count; loop++) {
		var reservation = reservations.childNodes[loop];
		var id = reservation.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		ids[loop] = id;
		
		// Get all the information from reservation xml
		var firstname = reservation.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
		
		var lastname;
		if (reservation.getElementsByTagName('lastname')[0].childNodes.length == 0)
			lastname = '';
		else
			lastname = reservation.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;

		var phone = reservation.getElementsByTagName('phone')[0].childNodes[0].nodeValue;
		if (phone == 'null')
			phone = '';
		
		var numPeople = reservation.getElementsByTagName('number')[0].childNodes[0].nodeValue;

		if (reservation.getElementsByTagName('createdby')[0].childNodes.length != 0) {
			var createdbyNode = reservation.getElementsByTagName('createdby')[0].childNodes[0];
			if (createdbyNode != null) {
				var empFirstname = createdbyNode.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
				var empLastname = createdbyNode.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;
				var createdBy = empFirstname + ' '+ empLastname;
			}
		}
		else 
			createdBy = firstname + ' ' + lastname;
			
		if (reservation.getElementsByTagName('placed')[0].childNodes.length > 0) {
			var placed = reservation.getElementsByTagName('placed')[0];
			var placedDay;
			if (placed.getElementsByTagName('day')[0].childNodes.length == 0)
				placedDay = '';
			else
				placedDay = placed.getElementsByTagName('day')[0].childNodes[0].nodeValue;
			
			
			var placedMonth;
			if (placed.getElementsByTagName('month')[0].childNodes.length == 0)
				placedMonth = '';
			else
				placedMonth = placed.getElementsByTagName('month')[0].childNodes[0].nodeValue;
			
			
			var placedYear;
			if (placed.getElementsByTagName('year')[0].childNodes.length == 0)
				placedYear = '';
			else
				placedYear = placed.getElementsByTagName('year')[0].childNodes[0].nodeValue;
		}
		
		if (reservation.getElementsByTagName('date')[0].childNodes.length > 0) {
			var date = reservation.getElementsByTagName('date')[0];
			var day = date.getElementsByTagName('day')[0].childNodes[0].nodeValue;
			var month = date.getElementsByTagName('month')[0].childNodes[0].nodeValue;
			var year = date.getElementsByTagName('year')[0].childNodes[0].nodeValue;
			var hour = date.getElementsByTagName('hour')[0].childNodes[0].nodeValue;
			var minute = date.getElementsByTagName('minute')[0].childNodes[0].nodeValue;
			if (minute.length == 1) {
				minute = '0' + minute;
			}
		}
		
		var description;
		if (reservation.getElementsByTagName('description')[0].childNodes.length == 0)
			description = '';
		else
			description = reservation.getElementsByTagName('description')[0].childNodes[0].nodeValue;
		
		var reservedDate = document.getElementById('d'+day+'m'+month+'y'+year);
		if (reservedDate != null) {
			var mon = parseInt(month) + 1;
			reservedDate.setAttribute('onclick', 'reportReservationByDate(' + day + ',' + mon + ',' + year + ', "day")');
			reservedDate.style.backgroundColor =  'aqua';
			//TODO change the css a litte more like hover. 
			reservedDate.style.cursor = 'pointer';
		}
		
		html += addResultRow(id, firstname, lastname, phone, numPeople, createdBy, placedDay,
				placedMonth, placedYear, day, month, year, hour, minute, description);
	}
	html += endResultsTable(ids);
	return html;
}

function addResultRow (id, firstname, lastname, phone, numPeople, createdBy, placedDay,
						placedMonth, placedYear, day, month, year, hour, minute, description) {
	var	html =
		'<form id="listReservationForm' + id + '">' +
		'<input id="firstname" value="' + firstname + '" type="hidden" />' +
		'<input id="lastname" value="' + lastname + '" type="hidden" />' +
		'<input id="phone" value="' + phone + '" type="hidden" />' +
		'<input id="numPeople" value="' + numPeople + '" type="hidden" />' +
		'<input id="createdBy" value="' + createdBy + '" type="hidden" />' +
		'<input id="placedDay" value="' + placedDay + '" type="hidden" />' +
		'<input id="placedMonth" value="' + placedMonth + '" type="hidden" />' +
		'<input id="placedYear" value="' + placedYear + '" type="hidden" />' +
		'<input id="day" value="' + day + '" type="hidden" />' +
		'<input id="month" value="' + month + '" type="hidden" />' +
		'<input id="year" value="' + year + '" type="hidden" />' +
		'<input id="hour" value="' + hour + '" type="hidden" />' +
		'<input id="minute" value="' + minute + '" type="hidden" />' +
		'<input id="description" value="' + description + '" type="hidden" />' +
		'</form>' +
		'<tr id="listReservationRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\')">' +
		'<td>' + firstname + " "+ lastname  + '</td>' +
		'<td class="alignRight">' + numPeople + '</td>' +
		'<td class="alignCenter">' + convertMonth(month) +' '+ day + ', ' + year + '</td>' +
		'<td class="alignCenter">' + hour +' : '+ minute + '</td>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}


function selectResultRow (id) {
	var ids = document.getElementById('resultsIds').value.split(',');
	var count = ids.length;
	for (var loop = 0; loop < count; loop++) {
		document.getElementById('listReservationRow' + ids[loop]).className = 'searchResultsRow';
	}
	try {
		document.getElementById('listReservationRow' + id).className = 'selectedResultsRow';
	} catch (e) {
	}
}

function showResult (id) {	
	selectResultRow(id);
	var frm = document.getElementById('listReservationForm' + id);
	
	document.getElementById('reservationID').value = id;
	document.getElementById('firstname').value = frm.firstname.value;
	document.getElementById('lastname').value = frm.lastname.value;
	document.getElementById('phone').value = frm.phone.value;
	document.getElementById('numPeople').value = frm.numPeople.value;
	document.getElementById('createdBy').innerHTML = frm.createdBy.value;
	
	// set the Date Placed field
	var dayPlaced = frm.placedDay.value;
	var monthPlaced = frm.placedMonth.value;
	
	var yearPlaced = frm.placedYear.value;
	document.getElementById('datePlaced').innerHTML = '<font size="3">' + dayPlaced +
		 ', ' + convertMonth(monthPlaced)+' ' + yearPlaced + '</font>';
	
	
	// set the Reservation Date/Time field
	dayList = document.getElementById('resDateDay');
	dayList.selectedIndex = frm.day.value -1;
	
	monthList = document.getElementById('resDateMonth');
	monthList.selectedIndex = frm.month.value;
	
	var year = frm.year.value;
	yearList = document.getElementById('resDateYear');
	for (var i = 0; i < yearList.length; i++){
		if (yearList.options[i].value == year){
			yearList.selectedIndex = i;
			break;
		}
	}
	
	var hour = frm.hour.value;
	hourList = document.getElementById('resDateHour');
	for (i = 0; i < hourList.length; i++){
		if (hourList.options[i].value == hour){
			hourList.selectedIndex = i;
			break;
		}
	}
	
	var minute = frm.minute.value;
	minuteList = document.getElementById('resDateMinute');
	for (i = 0; i < minuteList.length; i++){
		if (minuteList.options[i].value == minute){
			minuteList.selectedIndex = i;
			break;
		}
	}
	
	document.getElementById('description').value = frm.description.value;
	
	//clearResults();
}


function convertMonth(monthInt) {
	var month = null;
	switch (monthInt)
	{
		case '0': month = "January"; break;
		case '1': month = "February"; break;
		case '2': month = "March"; break;
		case '3': month = "April"; break;
		case '4': month = "May"; break;
		case '5': month = "June"; break;
		case '6': month = "July"; break;
		case '7': month = "August"; break;
		case '8': month = "September"; break;
		case '9': month = "October"; break;
		case '10': month = "November"; break;
		case '11': month = "December"; break;
	}
	return month;
}